{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# tuple  元组\n",
    "1.元组的定义方式：\n",
    "·（元素，元素，元素，......)\n",
    "\n",
    "2.元组的操作方法：\n",
    "index()       查找某个数据，如果数据存在返回对应下标，否则报错\n",
    "count()       统计某个数据在当前元组出现的次数\n",
    "len(元组)      统计元组内的元素个数\n",
    "\n",
    "3.元组的注意事项：\n",
    "不可修改内容    especially：元组里嵌套了list list里的元素可以修改\n",
    "\n",
    "4.元组特点：\n",
    "·和list基本相同（有序、任意数量元素、允许重复元素），唯一不同在于不可修改。\n",
    "·支持for循环\n",
    "\n",
    "多数特性和list一致，不同点在于不可修改的特性。"
   ],
   "id": "e1f9cdaa14886695"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "8cb41df5685c17be"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#定义元组\n",
    "t1 = (1,\"Hello\",True)\n",
    "t2 = ()\n",
    "t3 = tuple()\n",
    "print(f\"t1类型是：{type(t1)},内容是：{t1}\")\n",
    "print(f\"t1类型是：{type(t2)},内容是：{t2}\")\n",
    "print(f\"t1类型是：{type(t3)},内容是：{t3}\")\n",
    "\n",
    "#定义单个元素的元素\n",
    "t4 = (\"hello\",)\n",
    "print(f\"t4类型是：{type(t4)},内容是：{t4}\")\n",
    "\n",
    "#元组的嵌套\n",
    "t5=((1,2,3),(4,5,6))\n",
    "print(f\"t5类型是：{type(t5)},内容是：{t5}\")\n",
    "\n",
    "#下标索引取出内容\n",
    "num=t5[1][2]\n",
    "print(num)\n",
    "\n",
    "#元组的操作：index查找方法\n",
    "t6=(\"cao\",\"jia\",\"Python\")\n",
    "index = t6.index(\"jia\")\n",
    "print(f\"查找t6中jia的下标是：{index}\")\n",
    "\n",
    "#元组的操作：count统计方法\n",
    "t7=(\"cao\",\"jia\",\"jia\",\"Python\")\n",
    "num=t7.count(\"jia\")\n",
    "print(f\"在元组t7中jia的数量有：{num}个\")\n",
    "\n",
    "#元组的操作：len函数统计元组元素数量\n",
    "t8=(\"cao\",\"jia\",\"jia\",\"Python\")\n",
    "num=len(t8)\n",
    "print(f\"t8元组中元素有：{num}个\")\n",
    "\n",
    "#元组的遍历：while\n",
    "index=0\n",
    "while index<len(t8):\n",
    "    print(f\"元组的元素有：{t8[index]}\")\n",
    "    #至关重要\n",
    "    index+=1\n",
    "\n",
    "#元组的遍历：for\n",
    "for element in t8:\n",
    "    print(f\"元组的元素有：{element}\")\n",
    "\n",
    "#修改元组内容\n",
    "#t8[0]=\"jia++\"   报错\n",
    "\n",
    "#定义一个元组\n",
    "t9=(1,2,[\"jia\",\"+\"])\n",
    "print(f\"t9的内容是：{t9}\")\n",
    "t9[2][1]=\"jia\"\n",
    "print(f\"t9的内容是：{t9}\") "
   ],
   "id": "e434374a7bab9fc3"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#    str(字符串)",
   "id": "e43e7ecacedc75f0"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1.字符串为什么被称之为数据容器呢？",
   "id": "391c407851daee74"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "    字符串可以看做是字符的容器，支持下标索引等特性。",
   "id": "4db9f09b654668bb"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2.字符串有哪些常用操作方法？",
   "id": "97e70f2cc91f6269"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "    字符串.index(字符串)                查找给定字符的第一个匹配项的下标\n",
    "    字符串.replace(字符串1,字符串2)      将字符串内的全部字符串1，替换为字符串2 不会修改原字符串，而是得到一个新的         \n",
    "    字符串.split(字符串)                按照给定字符串对字符串进行分隔，不会修改原字符串，而是得到一个新的列表\n",
    "    字符串.strip()                     移除首尾的空格和换行符或指定字符串\n",
    "    字符串.strip(字符串)\n",
    "    字符串.count(字符串)                统计字符串内某字符串的出现次数  \n",
    "    len(字符串)                        统计字符串的字符个数"
   ],
   "id": "ffd94ac3447a441a"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 3.字符串有哪些特点：",
   "id": "831f9bcf9a6b7cc7"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "     ·只可以存储字符串\n",
    "     ·长度任意（取决于内存大小）\n",
    "     ·允许重复字符串存在\n",
    "     ·不可以修改（增加或删除元素等）\n",
    "     ·支持for循环"
   ],
   "id": "6285e4aa53936e97"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "my_str =\"jiajia learn python\"\n",
    "#通过下标索引取值\n",
    "value = my_str[2]\n",
    "value2=my_str[-16]\n",
    "print(f\"从字符串{my_str}取下标为2的元素，值是：{value}，取下标维-3的元素，值是{value2}\")\n",
    "\n",
    "#index方法\n",
    "value=my_str.index(\"learn\")\n",
    "print(f\"在字符串{my_str}中查找learn，其起始下标是：{value}\")\n",
    "\n",
    "#replace方法\n",
    "new_my_str=my_str.replace(\"learn\",\"学\")\n",
    "print(f\"将字符串{my_str},进行替换后得到：{new_my_str}\")"
   ],
   "id": "382ce5e6ea77bba7"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#split方法\n",
    "my_str=\"hello python hello world\"\n",
    "my_str_list=my_str.split(\" \")\n",
    "print(f\"将字符串{my_str}进行split切分后得到：{my_str_list},类型是{type(my_str_list)}\")"
   ],
   "id": "ca281b73fe3286b8"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#strip方法\n",
    "my_str=\" hello python hello world \"\n",
    "new_my_str=my_str.strip()             #不传入参数，去除首尾空格\n",
    "print(f\"字符串{my_str}被strip后，结果：{new_my_str}\")  \n",
    "\n",
    "my_str=\"12hello python hello world21\"\n",
    "new_my_str=my_str.strip(\"12\")\n",
    "print(f\"字符串{my_str}被strip('12')后，结果：{new_my_str}\") "
   ],
   "id": "9f0c3ef6ce4488e8"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### 从首从尾开始依次判断每个字符串是否是参数中包含的字符，如果是就去继续去除，否则就停止判断，和参数字符串顺序没有关系",
   "id": "4fd5340f19bce78"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "my_str=\"324124这是一个1234字符串314231\"\n",
    "new_my_str=my_str.strip(\"1234\")\n",
    "print(f\"字符串{my_str}被strip('1234')后，结果：{new_my_str}\")"
   ],
   "id": "25b34f33cf00b658"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#统计字符串中某字符串的出现次数，count\n",
    "my_str=\"hello python hello world\"\n",
    "count=my_str.count(\"h\")\n",
    "print(f\"字符串{my_str}中h出现的次数是：{count}\")\n",
    "\n",
    "#统计字符串的长度，len()\n",
    "num = len(my_str)\n",
    "print(f\"字符串{my_str}的长度是：{num}\")"
   ],
   "id": "738e137bcbbd7d6c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#遍历\n",
    "my_str=\"jia\"\n",
    "index=0\n",
    "while index < len(my_str):\n",
    "    print(my_str[index])\n",
    "    index+=1\n",
    "    \n",
    "for i in my_str:\n",
    "    print(i)"
   ],
   "id": "98a79b69457f61b7"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 练习：分割字符串",
   "id": "db47650e90c30809"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "        给定一个字符串：\"itheima itcast boxuegu\"\n",
    "        ·统计字符串内有多少个\"it\"字符\n",
    "        ·将字符串内的空格，全部替换为字符：\"|\"\n",
    "        ·并按照\"|\"进行字符串分割，得到列表\n",
    "     提示：·count、replace、split"
   ],
   "id": "df7b4594bebc4dec"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "my_str=\"itheima itcast boxuegu\"\n",
    "num=my_str.count(\"it\")\n",
    "print(f\"字符串{my_str}中it个数为{count}\")\n",
    "new_my_str=my_str.replace(\" \",\"|\")\n",
    "print(f\"字符串{my_str}被替换空格后，结果：{new_my_str}\")\n",
    "new_my_str=my_str.split(\"|\")\n",
    "print(f\"字符串{my_str}按照|分割后，结果：{new_my_str}\")"
   ],
   "id": "2b979a2cda0a1080"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数据容器（序列）的切片",
   "id": "90c56530be7d1c9f"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 1.序列\n",
    "        内容连续、有序，支持下标索引的一类数据容器\n",
    "### 2.可以视为序列的数据容器\n",
    "        列表、元组、字符串\n",
    "### 3.序列如何做切片\n",
    "        ·起始可以省略，省略从头开始\n",
    "        ·结束可以省略，省略到尾结束\n",
    "        ·步长可以省略，省略步长为1（可以为负数，表示倒序执行）"
   ],
   "id": "ce72f7d1bfdfb1d3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-07T06:33:32.063015Z",
     "start_time": "2024-12-07T06:33:32.046286Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#对list进行切片，从1开始，4结束，步长1\n",
    "my_list=[0,1,2,3,4,5,6]\n",
    "print(my_list[1:4])   #步长默认是1，可以省略不写\n",
    "\n",
    "#对tuple进行切片，从头开始，到最后结束，步长1\n",
    "my_tuple=(0,1,2,3,4,5,6)\n",
    "print(my_tuple[:])    #起始和结束不写表示从头到尾，步长为1可以省略\n",
    "\n",
    "#对str进行切片，从头开始，到最后结束，步长2\n",
    "my_str=\"01234567\"\n",
    "print(my_str[::2])\n",
    "\n",
    "#对str进行切片，从头开始，到最后结束，步长-1\n",
    "my_str=\"01234567\"\n",
    "print(my_str[::-1])   #等同于将序列反转了\n",
    "\n",
    "#对列表进行切片，从3开始，1结束，步长-1\n",
    "print(my_list[3:1:-1])\n",
    "\n",
    "#对元组进行切片，从头开始，到尾结束，步长-2\n",
    "print(my_tuple[::-2])"
   ],
   "id": "1fda39c002fd1917",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "(0, 1, 2, 3, 4, 5, 6)\n",
      "0246\n",
      "76543210\n",
      "[3, 2]\n",
      "(6, 4, 2, 0)\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 练习案例：序列的切片实践\n",
    "    有字符串：\"万过薪月，员序程马黑来，nohtyP学\"\n",
    "    ·使用学过的方式得到\"黑马程序员\"\n",
    "    \n",
    "    参考：\n",
    "    ·倒序字符串，切片取出或切片取出，然后倒序\n",
    "    ·split分隔“，”replace替换“来”为空，倒序字符串"
   ],
   "id": "4734640490cbc694"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-07T06:45:20.012893Z",
     "start_time": "2024-12-07T06:45:19.991760Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# str=\"万过薪月，员序程马黑来，nohtyP学\"\n",
    "# str2=str[::-1]\n",
    "# str3=str2[9:14:]\n",
    "# print(str3)\n",
    "\n",
    "my_str=\"万过薪月，员序程马黑来，nohtyP学\"\n",
    "#倒序，切片\n",
    "result1=my_str[::-1][9:14]\n",
    "print(f\"方式1结果：{result1}\")\n",
    "#切片，倒序\n",
    "result2=my_str[5:10][::-1]\n",
    "print(f\"方式2结果：{result2}\")\n",
    "#split\n",
    "result3=my_str.split(\"，\")[1].replace(\"来\",\"\")[::-1]\n",
    "print(f\"方式3结果：{result3}\")"
   ],
   "id": "8364e998c00dcb38",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方式1结果：黑马程序员\n",
      "方式2结果：黑马程序员\n",
      "方式3结果：黑马程序员\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 集合的定义和操作",
   "id": "2b6153e377cf5559"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "    1.集合的特点\n",
    "        ·集合内不允许重复元素（去重）\n",
    "        ·集合内元素是无序的（不支持下标索引）\n",
    "    2.集合的定义方式\n",
    "        {元素,元素,......,元素}\n",
    "    3.集合的常用操作\n",
    "        集合.add(元素)                  集合内添加一个元素\n",
    "        集合.remove(元素)               移除集合内指定的元素\n",
    "        集合.pop()                     从集合中随机抽取出一个元素\n",
    "        集合.clear()                   将集合清空\n",
    "        集合1.difference(集合2)         得到一个新集合，内含2个集合的差集 原有的2个集合内容不变\n",
    "        集合1.difference_update(集合2)  在集合1中，删除集合2中存在的元素 集合1被修改，集合2不变\n",
    "        集合1.union(集合2)              得到一个新集合，内含2个集合的全部元素 原有的2个集合内容不变\n",
    "        len(集合)                       得到一个整数，记录了集合的元素数量\n",
    "    4.遍历集合元素\n",
    "        ·可以使用for循环进行变量\n",
    "        ·不可以使用while循环，因为不支持下标索引\n",
    "    5.集合的索引\n",
    "        ·可以容纳多个数据\n",
    "        ·可以容纳不同类型的数据（混装）\n",
    "        ·数据是无需存储的（不支持下标索引）\n",
    "        ·不允许重复数据存在\n",
    "        ·可以修改（增加或删除元素等）\n",
    "        ·支持for循环        "
   ],
   "id": "54d3f7a75de6b114"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-07T07:41:48.094293Z",
     "start_time": "2024-12-07T07:41:48.069913Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#定义集合\n",
    "my_set={\"123\",\"456\",\"789\"}\n",
    "print(my_set)\n",
    "\n",
    "#添加新元素\n",
    "my_set.add(\"000\")\n",
    "print(my_set)\n",
    "\n",
    "#移除元素\n",
    "my_set.remove(\"000\")\n",
    "print(my_set)\n",
    "\n",
    "#随机取出一个元素\n",
    "element=my_set.pop()\n",
    "print(element)\n",
    "\n",
    "#清空集合\n",
    "my_set.clear()\n",
    "print(my_set)\n",
    "\n",
    "#取2个集合的差集\n",
    "set1={1,2,3}\n",
    "set2={1,5,6}\n",
    "set3=set1.difference(set2)   #set3=set1-set2\n",
    "print(set1,set2,set3)\n",
    "\n",
    "#消除2个集合的差集\n",
    "set1.difference_update(set2)\n",
    "print(set1)\n",
    "\n",
    "#2个集合合并为一个\n",
    "set1={1,2,3}\n",
    "set2={1,5,6}\n",
    "set4=set1.union(set2)\n",
    "print(set4)\n",
    "\n",
    "#统计集合元素数量len()\n",
    "set1={1,2,3,4,5,4,3,2,1}   #集合去重！！！\n",
    "num=len(set1)\n",
    "print(num)\n",
    "\n",
    "#集合的遍历\n",
    "#集合不支持下标索引，不能用while循环\n",
    "#可以用for循环\n",
    "for element in set1:\n",
    "    print(f\"集合的元素有{element}\")"
   ],
   "id": "c0ab4f13b668b741",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'123', '789', '456'}\n",
      "{'123', '789', '000', '456'}\n",
      "{'123', '789', '456'}\n",
      "123\n",
      "set()\n",
      "{1, 2, 3} {1, 5, 6} {2, 3}\n",
      "{2, 3}\n",
      "{1, 2, 3, 5, 6}\n",
      "5\n",
      "集合的元素有1\n",
      "集合的元素有2\n",
      "集合的元素有3\n",
      "集合的元素有4\n",
      "集合的元素有5\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 练习：信息去重\n",
    "    有如下列表对象：\n",
    "    my_list=['黑马程序员','传智播客','黑马程序员','传智播客','itheima','itcast','itheima','itcast','best']\n",
    "    请：\n",
    "    ·定义一个空集合\n",
    "    ·通过for循环遍历列表\n",
    "    ·在for循环中将列表的元素添加至集合\n",
    "    ·最终得到去重后的集合对象，并打印输出\n",
    "    "
   ],
   "id": "10d0d5649e87d20d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-07T08:08:35.704283Z",
     "start_time": "2024-12-07T08:08:35.691581Z"
    }
   },
   "cell_type": "code",
   "source": [
    "my_set=set()\n",
    "my_list=['黑马程序员','传智播客','黑马程序员','传智播客','itheima','itcast','itheima','itcast','best']\n",
    "for element in my_list:\n",
    "    my_set.add(element)\n",
    "print(my_set)"
   ],
   "id": "a62cd3fe05959f68",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'itheima', 'best', '传智播客', '黑马程序员', 'itcast'}\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# dict(字典、映射)\n",
    "### 字典的定义\n",
    "    1.字典可以提供基于Key检索Value的场景实现  similar~查字典\n",
    "    2.字典定义语法\n",
    "        #定义字典字面量\n",
    "        {key:value,key:value,......,key:value}\n",
    "        #定义字典面量\n",
    "        my_dict={key:value,key:value,......,key:value}\n",
    "        #定义空字典\n",
    "        my_dict={}          #空字典定义方式1\n",
    "        my_dict=dict()      #空字典定义方式2\n",
    "    3.字典注意事项\n",
    "        ·键值对的Key和Value可以是任意类型（Key不可为字典）\n",
    "        ·字典内Key不允许重复，重复添加等同于覆盖原有数据\n",
    "        ·字典不可用下标索引，而是通过Key检索Value"
   ],
   "id": "7740e951f1d70f94"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-07T11:49:51.800097Z",
     "start_time": "2024-12-07T11:49:51.776315Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#定义字典\n",
    "my_dict={\"陶吉吉\": 99,\"张三\":88,\"李四\":77}\n",
    "\n",
    "#定义空字典\n",
    "my_dict2={}      #set()\n",
    "my_dict3=dict()\n",
    "print(my_dict,my_dict2,my_dict3)\n",
    "\n",
    "#定义重复Key的字典\n",
    "my_dict4={\"陶吉吉\":99,\"陶吉吉\":88,\"123\":77}\n",
    "\n",
    "#从字典中基于Key获取Value\n",
    "score=my_dict[\"陶吉吉\"]\n",
    "print(f\"陶吉吉的分数是：{score}\")"
   ],
   "id": "ba4845982f7202f7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'陶吉吉': 99, '张三': 88, '李四': 77} {} {}\n",
      "陶吉吉的分数是：99\n",
      "学生考试信息{'张三': {'语文': 77, '数学': 66, '英语': 33}, '李四': {'语文': 88, '数学': 86, '英语': 55}, '陶吉吉': {'语文': 99, '数学': 96, '英语': 66}}\n"
     ]
    }
   ],
   "execution_count": 73
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "定义嵌套字典",
   "id": "2a634507fad26285"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-07T11:51:56.839328Z",
     "start_time": "2024-12-07T11:51:56.825281Z"
    }
   },
   "cell_type": "code",
   "source": [
    "stu_score_dict={\n",
    "    \"张三\":{\n",
    "        \"语文\":77,\n",
    "        \"数学\":66,\n",
    "        \"英语\":33\n",
    "    },\"李四\":{\n",
    "        \"语文\":88,\n",
    "        \"数学\":86,\n",
    "        \"英语\":55\n",
    "    },\"陶吉吉\":{\n",
    "        \"语文\":99,\n",
    "        \"数学\":96,\n",
    "        \"英语\":66\n",
    "    }\n",
    "}\n",
    "print(f\"学生考试信息{stu_score_dict}\")\n",
    "\n",
    "\n",
    "#从嵌套字典中获取数据\n",
    "score=stu_score_dict[\"陶吉吉\"][\"语文\"]\n",
    "print(f\"陶吉吉语文成绩为：{score}\")"
   ],
   "id": "ffa24ed03366ddc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学生考试信息{'张三': {'语文': 77, '数学': 66, '英语': 33}, '李四': {'语文': 88, '数学': 86, '英语': 55}, '陶吉吉': {'语文': 99, '数学': 96, '英语': 66}}\n",
      "99\n"
     ]
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "\n",
    "### 字典的常用操作\n",
    "    1.字典常用操作\n",
    "        字典[Key]            获取指定Key对应的Value值           \n",
    "        字典[Key]=Value      添加或更新键值对\n",
    "        字典.pop(Key)        取出Key对应的Value并在字典内删除此Key的键值对\n",
    "        字典.clear()         清空字典\n",
    "        字典.keys()          获取字典的全部Key,可用于for循环遍历字典\n",
    "        len(字典)            计算字典内的元素数量\n",
    "    2.操作注意\n",
    "        ·新增和更新元素的语法一致，如果Key不存在即新增，如果Key存在即更新（Key不可重复）\n",
    "    3.字典的特点\n",
    "        ·可容纳多个数据\n",
    "        ·可容纳不同类型的数据\n",
    "        ·每一份数据是KeyValue键值对\n",
    "        ·可以通过Key获取到Value,Key不可重复（重复会覆盖）\n",
    "        ·不支持下标索引\n",
    "        ·可以修改（增加或删除更新元素等\n",
    "        ·支持for循环，不支持while循环"
   ],
   "id": "cc0670e3c3cc730"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-07T12:15:50.323096Z",
     "start_time": "2024-12-07T12:15:50.297866Z"
    }
   },
   "cell_type": "code",
   "source": [
    "my_dict={\"陶吉吉\": 99,\"张三\":88,\"李四\":77}\n",
    "#新增元素\n",
    "my_dict[\"王五\"]=66\n",
    "print(my_dict)\n",
    "\n",
    "#更新元素\n",
    "my_dict[\"陶吉吉\"]=100\n",
    "print(my_dict)\n",
    "\n",
    "#删除元素\n",
    "score=my_dict.pop(\"李四\")\n",
    "print(my_dict,score)\n",
    "\n",
    "#清空元素,clear\n",
    "my_dict.clear()\n",
    "print(my_dict)\n",
    "\n",
    "#获取全部的Key\n",
    "my_dict={\"陶吉吉\": 99,\"张三\":88,\"李四\":77}\n",
    "print(my_dict.keys())\n",
    "\n",
    "#遍历字典\n",
    "##方式1:通过获取全部key来完成遍历\n",
    "keys=my_dict.keys()\n",
    "for key in keys:\n",
    "    print(key,my_dict[key])\n",
    "##方式2：直接对for循环，每一次循环都是直接得到key\n",
    "for key in my_dict:\n",
    "    print(key,my_dict[key])\n",
    "\n",
    "#统计字典内的元素数量,len()函数\n",
    "num=len(my_dict)\n",
    "print(f\"字典数量有{num}个\")"
   ],
   "id": "276c144928f10be8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'陶吉吉': 99, '张三': 88, '李四': 77, '王五': 66}\n",
      "{'陶吉吉': 100, '张三': 88, '李四': 77, '王五': 66}\n",
      "{'陶吉吉': 100, '张三': 88, '王五': 66} 77\n",
      "{}\n",
      "dict_keys(['陶吉吉', '张三', '李四'])\n",
      "陶吉吉 99\n",
      "张三 88\n",
      "李四 77\n",
      "陶吉吉 99\n",
      "张三 88\n",
      "李四 77\n",
      "字典数量有3个\n"
     ]
    }
   ],
   "execution_count": 85
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "fa41a082af81ccba"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 练习：升职加薪\n",
    "    有以下员工信息，请使用字典完成数据的记录，并通过for循环，对所有级别为1级的员工，级别上升1级，薪水增加1000元\n",
    "    姓名   部门  工资    级别\n",
    "    11     A   3000    1\n",
    "    22     B   5000    2\n",
    "    33     B   7000    3\n",
    "    44     A   4000    1\n",
    "    55     C   6000    2"
   ],
   "id": "cbe36620468af6cd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-07T12:44:12.395060Z",
     "start_time": "2024-12-07T12:44:12.381945Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#组织字典记录数据\n",
    "info_dict={\n",
    "    \"11\":{\n",
    "        \"部门\":\"A\",\n",
    "        \"工资\":3000,\n",
    "        \"级别\":1\n",
    "    },\"22\":{\n",
    "        \"部门\":\"B\",\n",
    "        \"工资\":5000,\n",
    "        \"级别\":2\n",
    "    },\"33\":{\n",
    "        \"部门\":\"B\",\n",
    "        \"工资\":7000,\n",
    "        \"级别\":3\n",
    "    },\"44\":{\n",
    "        \"部门\":\"A\",\n",
    "        \"工资\":4000,\n",
    "        \"级别\":1\n",
    "    },\"55\":{\n",
    "        \"部门\":\"C\",\n",
    "        \"工资\":6000,\n",
    "        \"级别\":2\n",
    "    }\n",
    "}\n",
    "print(f\"升职加薪前：{info_dict}\")\n",
    "#for循环遍历字典\n",
    "for name in info_dict:\n",
    "    #if条件判断符合条件的员工\n",
    "    if info_dict[name][\"级别\"]==1:\n",
    "        #升职加薪操作\n",
    "        employee_info_dict=info_dict[name]\n",
    "        employee_info_dict[\"级别\"]=2\n",
    "        employee_info_dict[\"工资\"]+=1000\n",
    "        info_dict[name]=employee_info_dict\n",
    "#输出结果\n",
    "print(f\"升职加薪后{info_dict}\")"
   ],
   "id": "94dd5912a1ea42f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "升职加薪前：{'11': {'部门': 'A', '工资': 3000, '级别': 1}, '22': {'部门': 'B', '工资': 5000, '级别': 2}, '33': {'部门': 'B', '工资': 7000, '级别': 3}, '44': {'部门': 'A', '工资': 4000, '级别': 1}, '55': {'部门': 'C', '工资': 6000, '级别': 2}}\n",
      "升职加薪后{'11': {'部门': 'A', '工资': 4000, '级别': 2}, '22': {'部门': 'B', '工资': 5000, '级别': 2}, '33': {'部门': 'B', '工资': 7000, '级别': 3}, '44': {'部门': 'A', '工资': 5000, '级别': 2}, '55': {'部门': 'C', '工资': 6000, '级别': 2}}\n"
     ]
    }
   ],
   "execution_count": 88
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 类数据容器的总结对比",
   "id": "1e25570883cab7c5"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "1.基于各类数据容器的特点，它们的应用场景如下：\n",
    "    ·列表：一组数据，可修改、可重复的存储场景\n",
    "    ·元组：一批数据，不可修改、可重复的存储场景\n",
    "    ·字符串：一串字符串的存储场景\n",
    "    ·集合：一批数据，去重存储场景\n",
    "    ·字典：一批数据，可用Key检索Value的存储场景\n"
   ],
   "id": "72a9f42bb9244b82"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 数据容器的通用操作\n",
   "id": "6d98ea03eed986d4"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
